﻿using FarmSmartSleep.Common.Domain.PM;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace FarmSmartSleep.Organ.API.Application.ProductionManagement.MotherGivingBirthFile
{
    /// <summary>
    /// 删除
    /// </summary>
    public class MotherGivingBirthDDeleteHandler:IRequestHandler<MotherGivingBirthDDeleteCommon, Result>
    {
        private readonly IBaseRepository<MotherGivingBirth> motherGivingBirthRep;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="motherGivingBirthRep"></param>
        public MotherGivingBirthDDeleteHandler(IBaseRepository<MotherGivingBirth> motherGivingBirthRep)
        {
            this.motherGivingBirthRep = motherGivingBirthRep;
        }

        public async Task<Result> Handle(MotherGivingBirthDDeleteCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CodeEnum.delete };
            try
            {
                var n = 0;
                var list = await motherGivingBirthRep.GetAll().FirstOrDefaultAsync(c => c.MotherGivingBirthId == request.motherGivingBirthId);
                list.Deletes = true;
                list.DeleteTime = DateTime.Now;
                list.DeleteId = request.CreateId;
                n += motherGivingBirthRep.Update(list);
                if (n != 0)
                {
                    result.Code = (int)CodeEnum.success;
                    result.Messege = "删除成功";
                }



            }
            catch (Exception)
            {
                result.Messege = "删除失败";
                throw;
            }
            return result;
        }
    }
}
